package com.kehutong.repair.dto;

import com.kehutong.repair.enums.Status;
import org.coraframework.json.annotation.JSONField;
import org.coraframework.orm.Entity;
import org.coraframework.orm.annotation.Column;
import org.coraframework.orm.annotation.UnSave;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Employee extends Entity {
    private static final long serialVersionUID = -6100721690681565264L;

    protected Employee(String id) {
        super(id);
    }

    @Column(comment="所属公司编号")
    private String companyNo;

    @Column(name="userid", comment="企业微信成员UserID")
    private String userid;

    @Column(comment="成员名称")
    private String name;

    @Column(comment="手机号码")
    private String mobile;

    @Column(comment="腾讯成员所属部门id列表")
    private List<String> department;

    @JSONField(serialize=false)
    @Column(comment="企业内部成员所属部门列表")
    private List<String> innerDepartment;

    @Column(comment="0:普通成员； 1:负责人")
    private List<String> is_leader_in_dept;

    @Column(comment="职务信息")
    private String position;

    @Column(comment="性别。0表示未定义，1表示男性，2表示女性")
    private String gender;

    @Column(length=127, comment="邮箱")
    private  String email;

    @Column(length=255, comment="头像url")
    private  String avatar;

    @Column(length=255, comment="头像缩略图url")
    private  String thumb_avatar;

    @Column(comment="座机")
    private  String telephone;

    @Column(comment="别名")
    private  String alias;

    @Column(comment="激活状态: 1=已激活，2=已禁用，4=未激活，5=退出企业")
    private Status status;

    @Column(length=127, comment="员工个人二维码")
    private String qr_code;

    @Column(length=64, comment="成员对外属性，字段详情见对外属性")
    private String external_profile;

    @Column(length=64, comment="对外职务，如果设置了该值，则以此作为对外展示的职务，否则以position来展示")
    private String external_position;

    @Column(length=64, comment="企业简称")
    private String external_corp_name;

    @Column(length=64, comment="地址")
    private String address;

    @Column(length=64, comment="支付用")
    private String open_userid;

    @Column(comment="主部门")
    private String main_department;

    @Column(comment="登录密码")
    private String password;

    @Column(comment="成员头像的mediaid")
    private String avatar_mediaid;

    @Column(comment="客户数量")
    private int customerNum;

    @Column(defValue = "NOT NULL", comment = "状态 true: 可用; 1:禁用")
    private boolean enable;

    @Column(comment="逻辑删除")
    private boolean deleted;

    @UnSave
    private List<String> departmentCode;

    @UnSave
    private List<String> departmentName;

    @UnSave
    private List<Department> departments;

    public List<Department> getDepartments() {
        return departments;
    }

    public void setDepartments(List<Department> departments) {
        this.departments = departments;
    }

    public List<String> getDepartmentCode() {
        return departmentCode;
    }

    public void setDepartmentCode(List<String> departmentCode) {
        this.departmentCode = departmentCode;
    }

    public List<String> getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(List<String> departmentName) {
        this.departmentName = departmentName;
    }

    public String getCompanyNo() {
        return companyNo;
    }

    public void setCompanyNo(String companyNo) {
        this.companyNo = companyNo;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public List<String> getDepartment() {
        return department;
    }

    public void setDepartment(List<String> department) {
        this.department = department;
    }

    public List<String> getInnerDepartment() {
        if (innerDepartment == null) {
            return Collections.emptyList();
        }
        return innerDepartment;
    }

    public void setInnerDepartment(List<String> innerDepartment) {
        this.innerDepartment = innerDepartment;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List<String> getIs_leader_in_dept() {
        return is_leader_in_dept;
    }

    public void setIs_leader_in_dept(List<String> is_leader_in_dept) {
        this.is_leader_in_dept = is_leader_in_dept;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getThumb_avatar() {
        return thumb_avatar;
    }

    public void setThumb_avatar(String thumb_avatar) {
        this.thumb_avatar = thumb_avatar;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public String getQr_code() {
        return qr_code;
    }

    public void setQr_code(String qr_code) {
        this.qr_code = qr_code;
    }

    public String getExternal_profile() {
        return external_profile;
    }

    public void setExternal_profile(String external_profile) {
        this.external_profile = external_profile;
    }

    public String getExternal_position() {
        return external_position;
    }

    public void setExternal_position(String external_position) {
        this.external_position = external_position;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getOpen_userid() {
        return open_userid;
    }

    public void setOpen_userid(String open_userid) {
        this.open_userid = open_userid;
    }

    public String getMain_department() {
        return main_department;
    }

    public void setMain_department(String main_department) {
        this.main_department = main_department;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public boolean isDeleted() {
        return deleted;
    }

    public void setDeleted(boolean deleted) {
        this.deleted = deleted;
    }

    public void addDepartment(String id) {
        if (Objects.isNull(department)) {
            department = new ArrayList<>();
        }

        if (!department.contains(id)) {
            department.add(id);
        }
    }

    public void addInnerDepartment(String id) {
        if (Objects.isNull(innerDepartment)) {
            innerDepartment = new ArrayList<>();
        }

        if (!innerDepartment.contains(id)) {
            innerDepartment.add(id);
        }
    }

    public void addLeader(String id) {
        if (Objects.isNull(is_leader_in_dept)) {
            is_leader_in_dept = new ArrayList<>();
        }

        if (!is_leader_in_dept.contains(id)) {
            is_leader_in_dept.add(id);
        }
    }

    public String getExternal_corp_name() {
        return external_corp_name;
    }

    public void setExternal_corp_name(String external_corp_name) {
        this.external_corp_name = external_corp_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static String toUserId(String companyNo, String userId) {
        return org.coraframework.authz.Subject.encrypt(companyNo + userId);
    }

    public String getAvatar_mediaid() {
        return avatar_mediaid;
    }

    public void setAvatar_mediaid(String avatar_mediaid) {
        this.avatar_mediaid = avatar_mediaid;
    }

    public boolean isLeader(String departmentNo) {
        List<String> depList = getInnerDepartment();
        for (int i = 0; i < depList.size(); i ++) {
            if (depList.get(i).equals(departmentNo)) {
                return "1".equals(is_leader_in_dept.get(i));
            }
        }

        return false;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getCustomerNum() {
        return customerNum;
    }

    public void setCustomerNum(int customerNum) {
        this.customerNum = customerNum;
    }
}
